package com.yenlien.traceyun.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;


public class ImgUtil {
    /**
     * 图片转成十六进制
     */
    public static String platePhotoURL(String name) {

//        String url = "/Users/jerry/fsdownload/";
//        String url="https://gateway.cpzsyun.com/plant/file/show/";
        String url = "/root/product/saas/plant/static/re/";
        File file = new File(url + name);
        if (file.exists()) {
            FileInputStream fs = null;
            FileChannel fc = null;
            String imge = "";
            try {
                fs = new FileInputStream(file);
                fc = fs.getChannel();
                if (fs.getChannel().size() <= 0) {
                    return "";
                }
                byte[] data = new byte[(int) fc.size()];
                int reads = 0;// 循环次数
                int re = 0; // 获取值
                while ((re = (int) fs.read()) != -1) {
                    data[reads] = (byte) re;
                    reads++;
                }
                fs.close();
                fc.close();
                imge = bytesToHexString(data);
                return imge;
            } catch (IOException e) {
//                LOG.error("图片获取失败获取失败" + e);
                e.getMessage();
            }
        } else {
            System.out.println("图片不存在，上传图片失败");
        }
        return "";
    }

    public static String bytesToHexString(byte[] src) {

        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();

    }


    /**
     * 十六进制转成图片
     */
    public static void saveToImgFile(String src, String output) {
        if (src == null || src.length() == 0) {
            return;
        }
        try {
            FileOutputStream out = new FileOutputStream(new File(output));
            byte[] bytes = src.getBytes();
            for (int i = 0; i < bytes.length; i += 2) {
                out.write(charToInt(bytes[i]) * 16 + charToInt(bytes[i + 1]));
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int charToInt(byte ch) {
        int val = 0;
        if (ch >= 0x30 && ch <= 0x39) {
            val = ch - 0x30;
        } else if (ch >= 0x41 && ch <= 0x46) {
            val = ch - 0x41 + 10;
        }
        return val;
    }

    public static void main(String[] args) {
        String imgName2 = "1624431778223_LLYRVL.png";

//        String str = platePhotoURL("/Users/jerry/fsdownload/l.png");
        String str = platePhotoURL(imgName2);
        System.out.println(str);
    }
}
